package cn.worldyao.homework.controller;


import cn.worldyao.homework.Tools;
import cn.worldyao.homework.entity.KeClass;
import cn.worldyao.homework.entity.StuClassR;
import cn.worldyao.homework.entity.Student;
import cn.worldyao.homework.service.KeClassService;
import cn.worldyao.homework.service.StuClassRService;
import cn.worldyao.homework.service.StudentService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaozi
 * @since 2020-05-11
 */
@Controller
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    KeClassService keClassService;
    @Autowired
    StuClassRService stuClassRService;

    @PostMapping("stureg")
    public ModelAndView sregister(@RequestBody(required = false) Student student) {
        log.info("\n-----sregister-----\n");
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        modelAndView.addObject("flag", false);
        if (!studentService.checkSLoginNameExist(student.getLoginName())) {
            student.setSalt(Tools.getSalt8());
            student.setPassword(Tools.MD5Pwd(student.getPassword(), student.getSalt()));
            student.setCreateTime(LocalDateTime.now());
            if (studentService.save(student)) {
                modelAndView.addObject("flag", true);
            }
        }
        return modelAndView;
    }


    @PostMapping("stuvalogName")
    @ResponseBody
    public ModelAndView stuValidateLoginName(@RequestParam String loginName) {
        log.info("\n-----tregister-----\n");
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        if (studentService.checkSLoginNameExist(loginName)) {
            modelAndView.addObject("msg", "用户名已存在");
            modelAndView.addObject("flag", false);
        } else {
            modelAndView.addObject("flag", true);
        }
        return modelAndView;
    }

    @PostMapping("stulogin")
    public String stuLogin(@RequestParam("userName") String username, @RequestParam("passWord") String password, HttpSession session) {
        log.info("\n-----stuLogin-----\n");
        Student student = new Student();
        student.setPassword(password);
        student.setLoginName(username);
        ModelAndView modelAndView = new ModelAndView();
        Student student1 = studentService.sLogin(student);
        if (student1 != null) {
            session.setAttribute("user", student1);
            modelAndView.addObject("flag", true);
            log.info("\n-----{}sLogin OK-----\n", student1);
            return "redirect:/sindex.html";
        } else {
            return null;
        }
    }


    @PostMapping("stushowallclass")
    @ResponseBody
    public String stuShowAllClass(HttpSession session) throws JsonProcessingException {
        log.info("\n-----stuShowAllClass-----\n");
        List<KeClass> list = keClassService.getAllClass();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(list);
//        if (student1 != null) {

//        } else {
//            return null;
//        }
        log.info(json);
        return json;
    }

    @PostMapping("stuapplyclass")
    @ResponseBody
    public String stuApplyClass(@RequestParam("classId") Integer classId, HttpSession session) throws JsonProcessingException {
        log.info("\n-----stuApplyClass-----\n");
        Object object = session.getAttribute("user");
        if (object == null) {
            return "redirect:/slogin.html";
        } else {
            if (object instanceof Student) {
                Student student = (Student) object;
                StuClassR stuClassR = new StuClassR();
                stuClassR.setClassId(classId);
                stuClassR.setStuId(student.getId());
                return String.valueOf(stuClassRService.save(stuClassR));
            }
        }
        return String.valueOf(false);
    }
}
